<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class Up
{
    protected $order;
    protected $set;
    protected $user;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->set = SetUserUp::getLastOne();
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 买卡升级自己
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {
            $this->upMy();
        }

        // 卡, 商品, 快递, 升级推荐链
        if ($order->express_area == 1
            || ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])
            || ProductAreaList::checkInArea($product_id, Product::$areaMap['BUY'])) {
            $this->upChain();
        }

    }

    public function upMy()
    {
        if ($this->user->level < UserLevel::$keys['vip1']) {
            $this->user->level = UserLevel::$keys['vip1'];

            // 过期时间
            $this->user->expired_vip = 1;
            $this->user->expired_date = date('Y-m-d', strtotime("+30 days"));

            $this->user->save();
        }

    }

    public function upChain()
    {
        $chain = Chain::getReferrerChain($this->user->id, [], true);

        foreach ($chain as $user) {

            // 不是VIP, 或者是临时VIP
            if ($user->level < UserLevel::$keys['vip1'] || (
                    $user->level == UserLevel::$keys['vip1'] && $user->expired_vip = 1
                )) {
                // $has_vip1 = (int)Chain::getChildrenByLevel($user->id, [
                //     UserLevel::$keys['vip1'],
                //     UserLevel::$keys['vip2'],
                //     UserLevel::$keys['vip3'],
                //     UserLevel::$keys['vip4'],
                // ]);
                $ids = Chain::getChildren($user->id);
                $ids = ArrayHelper::getColumn($ids, 'id');

                // 这里必须用下单处理, 否则会产生链条升级
                $has_vip1 = Order::find()
                    ->where(['user_id' => $ids])
                    ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
                    ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
                    ->select(['user_id'])
                    ->groupBy(['user_id'])
                    ->asArray()
                    ->all();
                // var_dump($has_vip1);exit;

                $has_vip1 = count($has_vip1);
                // var_dump($has_vip1);exit;

                if ($has_vip1 >= $this->set->up_vip1) {
                    $user->level = UserLevel::$keys['vip1'];
                    $user->expired_vip = 0;
                    $user->save();
                }
            }

            // 升级高等级, 必须是VIP
            if ($user->level < UserLevel::$keys['vip1']) {
                // continue;
            }

            // 升级SVIP, 两种条件
            if ($user->level < UserLevel::$keys['vip2']) {

                $has_vip2 = Chain::getChildrenByLevel($user->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                ]);
                $has_vip2 = count($has_vip2);
                if ($has_vip2 >= $this->set->up_vip2) {
                    $user->level = UserLevel::$keys['vip2'];
                    $user->save();
                } else if ($has_vip2 >= $this->set->up_vip2_first) {
                    $has_vip2_team = Chain::getChildrenAllQuick($user->id, [
                        UserLevel::$keys['vip1'],
                        UserLevel::$keys['vip2'],
                        UserLevel::$keys['vip3'],
                        UserLevel::$keys['vip4'],
                    ]);
                    $has_vip2_team = count($has_vip2_team);
                    if ($has_vip2_team >= $this->set->up_vip2_team) {
                        $user->level = UserLevel::$keys['vip2'];
                        $user->save();
                    }
                }
            }

            // 市代
            if ($user->level < UserLevel::$keys['vip3']) {
                $has_vip3 = Chain::getChildrenByLevel($user->id, [
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                ]);
                $has_vip3 = count($has_vip3);
                if ($has_vip3 >= $this->set->up_vip3) {
                    $$user->level = UserLevel::$keys['vip3'];
                    $user->save();
                }
            }

            // 省代
            if ($user->level < UserLevel::$keys['vip4']) {
                $has_vip4 = Chain::getChildrenByLevel($user->id, [
                    UserLevel::$keys['vip3'],
                    UserLevel::$keys['vip4'],
                ]);
                $has_vip4 = count($has_vip4);
                if ($has_vip4 >= $this->set->up_vip4) {
                    $user->level = UserLevel::$keys['vip4'];
                    $user->save();
                }
            }
        }
    }


}